package com.ruoyi.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;

import com.ruoyi.business.domain.CourseInfo;
import com.ruoyi.business.domain.bo.CourseInfoBo;
import com.ruoyi.business.domain.vo.ChapterDetailVo;
import com.ruoyi.business.domain.vo.CourseInfoVo;
import com.ruoyi.business.domain.vo.VideoDetailVo;
import com.ruoyi.business.service.ICourseInfoService;
import com.ruoyi.business.service.ICourseUserService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.vo.InnerObjVo;
import io.swagger.annotations.ApiOperation;
import javafx.scene.control.Tab;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程信息Controller
 * 
 * @author 最爱喝兽奶
 * @date 2022-07-16
 */
@RestController
@RequestMapping("/business/course")
public class CourseInfoController extends BaseController
{
    @Autowired
    private ICourseInfoService tCourseInfoService;

    @Autowired
    private ICourseUserService courseUserService;

    @Autowired
    private RedisCache redisCache;


    /**
     * 查询课程信息列表
     */
    @GetMapping("/list")
    public AjaxResult list(CourseInfoBo tCourseInfo)
    {
        TableDataInfo<CourseInfoVo> list = tCourseInfoService.selectTCourseInfoList(tCourseInfo);
        return AjaxResult.success(list);
    }

    /**
     * 导出课程信息列表
     */
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseInfoBo tCourseInfo)
    {
        TableDataInfo<CourseInfoVo> list = tCourseInfoService.selectTCourseInfoList(tCourseInfo);
        ExcelUtil<CourseInfoVo> util = new ExcelUtil<CourseInfoVo>(CourseInfoVo.class);
//        util.exportExcel(response, list.getRows(), "课程信息数据");
    }

    /**
     * 获取课程信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:INFO:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tCourseInfoService.selectTCourseInfoById(id));
    }

    /**
     * 新增课程信息
     */
    @PreAuthorize("@ss.hasPermi('system:INFO:add')")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseInfoBo tCourseInfo) {
        try {
            String id = tCourseInfoService.insertTCourseInfo(tCourseInfo);
            return AjaxResult.success(id);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }


    /**
     * 修改课程信息
     */
    @PreAuthorize("@ss.hasPermi('system:INFO:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseInfoBo tCourseInfo)
    {
        return AjaxResult.success(tCourseInfoService.updateTCourseInfo(tCourseInfo));
    }


    /**
     * 修改课程状态
     */
    @PreAuthorize("@ss.hasPermi('system:INFO:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody CourseInfoBo tCourseInfo)
    {
        // 指派班级
        tCourseInfoService.assignClassForCourse(tCourseInfo);
        return AjaxResult.success(tCourseInfoService.updateTCourseStatus(tCourseInfo));
    }


    /**
     * 修改课程状态
     */
    @PreAuthorize("@ss.hasPermi('system:INFO:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PostMapping("/offStatus")
    public AjaxResult offStatus(@RequestBody CourseInfoBo tCourseInfo)
    {
        return AjaxResult.success(tCourseInfoService.offTCourseStatus(tCourseInfo));
    }

    /**
     * 删除课程信息
     */
    @PreAuthorize("@ss.hasPermi('system:INFO:remove')")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tCourseInfoService.deleteTCourseInfoByIds(ids));
    }

    @Log(title = "课程信息", businessType = BusinessType.OTHER)
    @GetMapping("/ref/teacherId")
    public AjaxResult queryRefCourseInfo(@RequestParam(value = "teacherId") String teacherId) {
        List<InnerObjVo> list = tCourseInfoService.queryRefCourseInfo(teacherId);
        return AjaxResult.success(list);
    }


    @RequestMapping("submitAll")
    public AjaxResult submitAll(@RequestBody CourseInfoVo tCourseVo) {
        System.out.println("开始上传");
        CourseInfo courseInfo = new CourseInfo();

        LoginUser loginUser = getLoginUser();
        String userId = loginUser.getUserId();
        String username = loginUser.getUsername();
        tCourseVo.setUploadTeacher(username);
        tCourseVo.setUploadTime(DateUtils.getDate());
        BeanUtils.copyProperties(tCourseVo,courseInfo);

        tCourseInfoService.insertTCourseInfo(null);
        return null;
    }

    @RequestMapping("queryCourse")
    public AjaxResult queryCourse() {
        CourseInfo courseInfo = new CourseInfo();
        LoginUser loginUser = getLoginUser();
        String userId = loginUser.getUserId();
        String username = loginUser.getUsername();
        List<InnerObjVo> innerObjVos = tCourseInfoService.queryRefCourseInfo(userId);
        return AjaxResult.success(innerObjVos);
    }



    @GetMapping("/getCourseIndex")
    public TableDataInfo getAllCourse( CourseInfoBo bo) {

        startPage();

        List<CourseInfoVo> list = tCourseInfoService.selectAll(bo);


        return getDataTable(list);
    }

    @GetMapping(value = "/queryCourse/{id}")
    public AjaxResult getOneCourse(@PathVariable("id") String id) {


        List<CourseInfoVo> list = tCourseInfoService.selectOneCourse(id);
        // 查询报名人数
        Long num = courseUserService.selectCountByCourseId(id);
        CourseInfoVo vo = list.get(0);
        vo.setNum(num);
        return AjaxResult.success(vo);
    }


    @GetMapping(value = "/queryCourseBySeries/{id}")
    public TableDataInfo getCourseBySeries(@PathVariable("id") String id) {

        startPage();

        List<CourseInfoVo> list = tCourseInfoService.selectCourseBySeries(id);


        return getDataTable(list);
    }



    @GetMapping(value = "/queryChapter/{id}")
    public AjaxResult getChapterDetail(@PathVariable("id") String id) {

        List<ChapterDetailVo> list = tCourseInfoService.selectCourseChapter(id);

        return AjaxResult.success(list);
    }

    @GetMapping(value = "/queryVideo/{id}")
    public AjaxResult getVideoDetail(@PathVariable("id") String id) {

        String url = tCourseInfoService.SelectVideoUrl(id);
        String name = tCourseInfoService.SelectVideoName(id);
        VideoDetailVo vo = new VideoDetailVo();
        vo.setId(id);
        vo.setVideoName(name);
        vo.setUrl(url);

        return AjaxResult.success(vo);
    }
    /**
     * 分页查询课程关联对象
     */
    @ApiOperation("查询课程关联对象")
    @GetMapping("/refTable")
    public AjaxResult refTable(@NotEmpty(message = "关联对象不能为空") String refColumn, Integer pageSize, Integer currentPage, String filterContent, String filterId) {
        return AjaxResult.success(tCourseInfoService.selectRefTableByName(refColumn, pageSize, currentPage, filterContent, filterId));
    }


    @PostMapping(value = "subScribeCourse/{id}")
    public AjaxResult subScribeCourse(@PathVariable("id") String id) {
        Boolean flag = tCourseInfoService.subScribeCourse(id);
        if (flag) {
            return AjaxResult.success("提交成功");
        } else {
            return AjaxResult.error("提交失败");

        }
    }

    @GetMapping(value = "queryIfSubScribe/{id}")
    public AjaxResult queryIfSubScribe(@PathVariable("id") String id) {
        Boolean flag = tCourseInfoService.queryIfSubScribe(id);
        if (flag) {
            return AjaxResult.success(200);
        }else {
            return AjaxResult.success(500);
        }
    }

}



